<template>
  <div style="width: 100%">
    <el-row style="flex-wrap: nowrap">
      <template v-for="(item, index) in configList" style="width: 100%">
        <span
          v-if="item.configType === 0"
          style="line-height: 32px; margin-right: 5px; margin-left: 5px"
        >
          {{ item.configValue }}
        </span>
        <span
          v-if="item.configType === 5"
          style="line-height: 32px; margin-right: 5px; margin-left: 5px"
        >
          {{ form.strMaxValue }}
        </span>
        <el-input
          v-model="form[item.configId]"
          :placeholder="'请输入' + item.configName"
          v-if="item.configType === 2"
          :style="
            configList.length == 1 ? 'width: 100%' : 'width:140px;margin-right: 5px;'
          "
          clearable
        />
        <el-date-picker
          v-model="form[item.configId]"
          placeholder="请选择日期"
          v-if="item.configType === 3 && !item.writeType"
          style="width: 140px; margin-right: 5px"
          @change="formDataChange"
          :clearable="false"
          :editable="false"
          :type="item.configName == 'YYYY' ? 'year' : 'date'"
          :value-format="item.configName"
          :format="item.configName"
        />
        <span
          v-if="item.configType === 3 && item.configValue"
          style="line-height: 32px; margin-right: 5px; margin-left: 5px"
          >{{ item.configValue }}</span
        >
        <span
          v-if="item.configType === 1 && item.sysSerialOptionList?.length == 1"
          style="line-height: 32px; margin-right: 5px; margin-left: 5px"
          >{{ item.sysSerialOptionList[0].label }}</span
        >
        <el-select
          v-if="
            (item.configType === 4 || item.configType === 1) &&
            item.sysSerialOptionList?.length != 1
          "
          v-model="form[item.configId]"
          @change="formDataChange"
          style="width: 140px"
          ilterable
        >
          <el-option
            v-for="(item1, index) in item.sysSerialOptionList"
            :key="index"
            :value="item1.value"
            :label="item1.label"
            >{{ item1.label }}</el-option
          >
        </el-select>
      </template>
    </el-row>

    <div
      style="
        color: var(--theme-deep-color);
        font-size: 12px;
        height: 12px;
        line-height: 1;
        position: absolute;
      "
    >
      {{ form.maxCode }}
    </div>
    <div style="font-size: 14px; color: red">{{ textNoNext }}</div>
  </div>
</template>

<script setup>
import { listSerialConfig, getSerialNum } from "@/api/system/serial";
import { getCurrentTime } from "@/api/common";
const { proxy } = getCurrentInstance();
const configList = ref([]);
const props = defineProps({
  typeId: {
    type: Number,
    default: -1,
  },
  paramsData: {
    type: Object,
    default: {},
  },
});
const form = ref({});

watch(
  () => props.typeId,
  () => {
    if (props.typeId != -1) {
      form.value = {};
      form.value.typeId = props.typeId;
      getList();
    } else {
      configList.value = [];
    }
  },
  { deep: true, immediate: true }
);

function reset() {
  form.value = {
    typeId: props.typeId,
  };
}

const textNoNext = ref("");
function getList() {
  let temp = {
    typeId: form.value.typeId,
  };
  temp.params = {};
  temp.params = props.paramsData;
  listSerialConfig(temp).then(async (res) => {
    if (!res.data || res.data.length == 0) {
      textNoNext.value = "缺失流水号规则！";
      configList.value = [];
      return false;
    } else {
      textNoNext.value = "";
    }
    // let next = false;
    // for (const item of res.data) {
    //   if (item.configType == 5) {
    //     next = true;
    //     textNoNext.value = "";
    //     break;
    //   }
    // }
    configList.value = res.data;
    let isAuto = true;
    for (const item of configList.value) {
      // if (item.configType == 2){
      //   isAuto = false;
      // }
      if (item.configType == 3) {
        //日期类型
        if (item.configValue) {
          form.value[item.configId] = item.configValue;
        } else {
          item.configName = item.configName.toUpperCase();
          isAuto = false;
        }
      } else if (
        (item.configType == 1 || item.configType == 4) &&
        item.sysSerialOptionList != 1
      ) {
        //configType:1-sql字段 4-选择字段。
        if (item.sysSerialOptionList?.length > 0)
          form.value[item.configId] = item.sysSerialOptionList[0].value;
        else isAuto = false;
      }
    }
    if (isAuto) {
      getLsh();
    }
  });
}

function getLsh() {
  let temp = form.value;
  Object.assign(temp, props.paramsData);
  getSerialNum(temp).then((res) => {
    debugger;
    if (res.data.strMaxValue) {
      form.value.strMaxValue = res.data.strMaxValue;
    } else {
      delete form.value.strMaxValue;
    }
    if (res.data.maxCode) {
      form.value.maxCode = res.data.maxCode;
    } else {
      delete form.value.maxCode;
    }
    emits("emitForm", form.value);
  });
}

const emits = defineEmits(["emitForm"]);

function formDataChange() {
  debugger;
  let isGet = true;
  for (const item of configList.value) {
    if (
      item.configType === 4 ||
      item.configType === 1 ||
      (item.configType === 3 && !item.configValue)
    ) {
      if (!form.value[item.configId]) {
        isGet = false;
        break;
      }
    }
  }
  if (isGet) {
    getLsh();
  }
}
// 记得暴露
defineExpose({
  reset,
  getList,
});
</script>

<style></style>
